//
//  GestureRecognizer.m
//  NXTRemoteController
//
//  Created by naceka on 06.05.12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "GestureView.h"


@implementation GestureView

- (GestureView *) initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	draw = false;
	count = 0;
	return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"TouchesBegan!");
	UITouch *touch = [touches anyObject];
		
	count = 0;
	draw = true;
	
	//process point
    lastPoint = [touch locationInView:self];
	[self processPoint:&lastPoint];
	
	[self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"TouchesMoved!");
	UITouch *touch = [touches anyObject];	
	
	lastPoint = [touch locationInView:self];
	[self processPoint:&lastPoint];
	
	[self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"TouchesEnd!");
	//draw = false;
	[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {

	if (draw) {
		for (int i = 0; i < count; i++) {
			CGRect r = CGRectMake(points[i].x - 3, points[i].y - 3, 6, 6);
			/* Draw a circle */
			// Get the contextRef
			CGContextRef contextRef = UIGraphicsGetCurrentContext();
			
			//border width
			float width = 1.0;
			float red = 0.0, green = 0.0, blue = 0.0;
			
			if (i == 0 || i == count - 1) {
				width = 4.0;
				
				if (i == 0) {
					green = 255.0;
				}
				else {
					red = 255.0;
				}
			}
			
			// Set the border width
			CGContextSetLineWidth(contextRef, width);
			// Set the cicle border color
			CGContextSetRGBStrokeColor(contextRef, red, green, blue, 1.0);
			// Set the circle fill color to BLACK
			CGContextSetRGBFillColor(contextRef, 0.0, 0.0, 0.0, 1.0);
			// Fill the circle with the fill color
			CGContextFillEllipseInRect(contextRef, r);
			// Draw the circle border
			CGContextStrokeEllipseInRect(contextRef, r);
			
			if (i > 0) {
				CGContextSetStrokeColorWithColor(contextRef, [UIColor blackColor].CGColor);
				CGContextSetLineWidth(contextRef, 1.0);
				
				//draw line
				CGContextMoveToPoint(contextRef, points[i - 1].x, points[i - 1].y);
				CGContextAddLineToPoint(contextRef, points[i].x, points[i].y);
				
				//draw it
				CGContextStrokePath(contextRef);
			}
		}		
	}
}

- (void) clear {
	count = 0;
	draw = false;
	[self setNeedsDisplay];
}

- (CGPoint *) getPoints {
	return points;
}

- (int) getCount {
	return count;
}

- (void) approximatePoint:(CGPoint *) p {
	//save start data
	CGPoint temp = *p;
	
	p->x = ((int)p->x) / 100 * 100;
	p->y = ((int)p->y) / 100 * 100;
	
	//check for diagonal
	if (count > 0 && p->x != points[count - 1].x && p->y != points[count - 1].y ) {
		CGPoint lp = points[count - 1];
		
		//compare x and y abs
		if (abs((int)(temp.x - lp.x)) < abs((int)(temp.y - lp.y))) {
			p->x = lp.x;
		}
		else {
			p->y = lp.y;
		}
	}
	
	
	if (p->x == 0.0) {
		p->x += 100;
	}
	if (p->x == 800.0) {
		p->x -= 100;
	}
	if (p->y == 0.0) {
		p->y += 100;
	}
	if (p->y == 600.0) {
		p->y -= 100;
	}	
}

- (void) processPoint:(CGPoint *) p {
	[self approximatePoint:p];
	
	//current and previous point compare
	if (count > 0 && points[count - 1].x == p->x && points[count - 1].y == p->y ) {
		//DO NOTHING
	} else {
		points[count] = *p;
		count++;
	}
}

@end
